//
//  YXDownloadingTableViewCell.m
//  MockProject
//
//  Created by FanYu on 23/8/2016.
//  Copyright © 2016 niuzhaowang. All rights reserved.
//

#import "YXDownloadingTableViewCell.h"
#import "YXUserDefault.h"
#import "YXGradientView.h"

@interface YXDownloadingTableViewCell ()

@property (nonatomic, strong) YXGradientView *gradientView;
@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) CAGradientLayer *gradientLayer;

@end


@implementation YXDownloadingTableViewCell

- (IBAction)actionButtonTapped:(UIButton *)sender {
    [self.delegate downloadingCellButtonTapped:sender];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    
    // title
    self.titleLabel.textColor = [UIColor colorWithHexString:@"403e3d"];
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    
    // size
    self.sizeLabel.textColor = [UIColor colorWithHexString:@"a3a1a0"];
    self.sizeLabel.font = [UIFont systemFontOfSize:12];
    
    // speed
    self.speedLabel.textColor = [UIColor colorWithHexString:@"e59252"];
    self.speedLabel.font = [UIFont systemFontOfSize:12];
    
    // button
    [self.actionButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.actionButton setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [self.actionButton setTitleColor:[UIColor colorWithHexString:@"797675"] forState:UIControlStateNormal];
    [self.actionButton setTitleColor:[UIColor colorWithHexString:@"ff7000"] forState:UIControlStateHighlighted];
    [self.actionButton.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [self.actionButton sizeToFit];
    self.actionButton.layer.cornerRadius = 12;
    self.actionButton.backgroundColor = [UIColor colorWithHexString:@"f9f9f9"];
    
    // progress
    self.grayProgressView.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
    self.progressView.backgroundColor = [UIColor whiteColor];
    
    // gradient
    self.gradientView = [[YXGradientView alloc] init];
    [self.progressView addSubview:self.gradientView];
    [self.gradientView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.progressView);
    }];
    
    self.maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 2)];
    self.maskView.backgroundColor = [UIColor whiteColor];
    self.progressView.maskView = self.maskView;
    
    self.progress = 0.0;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@synthesize progress = _progress;
- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    [self setNeedsLayout];
}

- (CGFloat)progress {
    return _progress;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect maskRect = [self.maskView frame];
    maskRect.size.width = CGRectGetWidth(self.bounds) * self.progress;
    self.maskView.frame = maskRect;
}

- (void)setTaskModel:(YXTaskModel *)taskModel {
    _taskModel = taskModel;
    
    self.titleLabel.text = taskModel.title;
    self.sizeLabel.text = taskModel.sizePercent;
    self.speedLabel.text = taskModel.speed;
    self.status = taskModel.status;
    self.actionButton.tag = taskModel.uid.integerValue;
    self.progress = taskModel.progress.floatValue;
    
    if ([taskModel.status isEqualToNumber:[NSNumber numberWithInteger:YXTaskStatusDownloading]]) {
        [self.actionButton setImage:[UIImage imageNamed:@"下载中下载"] forState:UIControlStateNormal];
        [self.actionButton setImage:[UIImage imageNamed:@"下载中icon点击态"] forState:UIControlStateHighlighted];
        [self.actionButton setTitle:@" 下载中" forState:UIControlStateNormal];
    } else if ([taskModel.status isEqualToNumber:[NSNumber numberWithInteger:YXTaskStatusPause]]) {
        [self.actionButton setImage:[UIImage imageNamed:@"已暂停icon正常态"] forState:UIControlStateNormal];
        [self.actionButton setImage:[UIImage imageNamed:@"已暂停icon点击态"] forState:UIControlStateHighlighted];
        [self.actionButton setTitle:@" 已暂停" forState:UIControlStateNormal];
    } else if ([taskModel.status isEqualToNumber:[NSNumber numberWithInteger:YXTaskStatusWaiting]]) {
        [self.actionButton setImage:[UIImage imageNamed:@"等待中icon正常态"] forState:UIControlStateNormal];
        [self.actionButton setImage:[UIImage imageNamed:@"等待中icon点击态"] forState:UIControlStateHighlighted];
        [self.actionButton setTitle:@" 等待中" forState:UIControlStateNormal];
    } else if ([taskModel.status isEqualToNumber:[NSNumber numberWithInteger:YXTaskStatusFailed]]) {
        [self.actionButton setImage:[UIImage imageNamed:@"下载失败icon正常态"] forState:UIControlStateNormal];
        [self.actionButton setImage:[UIImage imageNamed:@"下载失败icon点击态"] forState:UIControlStateHighlighted];
        [self.actionButton setTitle:@" 下载失败" forState:UIControlStateNormal];
    } else if ([taskModel.status isEqualToNumber:[NSNumber numberWithInteger:YXTaskStatusPrepare]]) {
        [self.actionButton setImage:[UIImage imageNamed:@"等待中icon正常态"] forState:UIControlStateNormal];
        [self.actionButton setImage:[UIImage imageNamed:@"等待中icon点击态"] forState:UIControlStateHighlighted];
        [self.actionButton setTitle:@" 准备中" forState:UIControlStateNormal];
    }
}

@end
